package cz.acies.face.modul;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.google.gson.Gson;

import cz.acies.data.dao.MModelFolderDAO;
import cz.acies.data.persist.TreeNode;
import cz.acies.face.viewers.TreeViewer;

public class ModulFolderView extends TreeViewer
{
	private static final long serialVersionUID = 5530532574840799514L;
	
	public ModulFolderView() { 
		super(); 
	}

	
	@Override
	public Object getInput() {
		items = new ArrayList<TreeNode>();
		try {
			Class<?> c = Class.forName(getFolderDaoClassName());
			MModelFolderDAO folderDao = (MModelFolderDAO) c.newInstance();
			items = folderDao.createFolders(getStructureTables()); 
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		return items;
	}

	protected String createAppendAction() {
		return "folderNew();";
	}

	protected String createDeleteAction() {
		return "folderDelete();";
	}

	@Override
	public String toHtml() {
		Gson gson = new Gson();
		String html = 
	    "<div id=\"foldersCtxMenu\" class=\"easyui-menu\" style=\"width:120px;\">" +
			"<div onclick=\"folderNew();\" data-options=\"iconCls:'icon-new'\">Nový</div>" +
			"<div onclick=\"folderDelete()\" data-options=\"iconCls:'icon-delete'\">Smazat</div>" +
			"<div class=\"menu-sep\"></div>"+
			"<div onclick=\"folderUpdate()\" data-options=\"iconCls:'icon-edit'\">Vlasnosti</div>" +
		"</div>" +
		
		"<div id=\""+this.id+"\" data-options=\"region:'west',border:true\" style=\"width:200px; padding-top: 10px;\">" + 
			"<ul id=\"folderTree\" class=\"easyui-tree\"></ul>" + 
			"<script type=\"text/javascript\">" + 
				"$(\"#"+this.id+"\").tree({" + 
					"data: "+gson.toJson(getInput())+"," + 
					"onContextMenu: function(e, node){" + 
						"e.preventDefault();" + 
						"$(\"#"+this.id+"\").tree(\"select\", node.target);" + 
						"$(\"#foldersCtxMenu\").menu(\"show\", {" + 
							"left: e.pageX," + 
							"top: e.pageY" + 
						"});" + 
					"}," + 
				"onSelect: function(node){" + 
					"if (node.attributes.selectable) {" + 
						 
					"}" + 
				"}" + 
			"});" + 
			/*	
			"function folderNew() {" +
		    	createAppendAction() + 
		    "}" +
		    "function folderDelete() {" +
		    	createDeleteAction() +
		    "}" +
			 */ 
			"</script>" +
		"</div>"; 

		return html;
	}

	// -----  -----------------------------------------------------------------

	protected List<String> getStructureTables() {
		return new Vector<String>();
	}

	protected String getFolderDaoClassName() {
		return "";
	}

}
